package mortal.game.control
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Game.*;
    import Message.Public.*;
    import flash.events.*;
    import modules.*;
    import mortal.component.window.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene.events.*;
    import mortal.game.scene.layer.utils.*;
    import mortal.game.scene.map.*;
    import mortal.game.scene.player.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.scene.player.info.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class NpcShopMallController extends Controller
    {
        private var _npcShopMallModule:NPCShopMallModule;
        private var _npcShopInfo:NpcShopInfo;

        public function NpcShopMallController()
        {
            Dispatcher.addEventListener(EventName.OpenNpcShopMall, this.onOpenNpcShopMallHandler);
            Dispatcher.addEventListener(EventName.ShopBuyNpcItem, this.onShopBuyNpcItemHandler);
            Dispatcher.addEventListener(EventName.ShopBuyNpcItemSuccess, this.onShopBuyNpcItemSuccessHandler);
            RolePlayer.instance.addEventListener(PlayerEvent.GIRD_WALK_END, this.onGirdWalkEnd);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            
        }

        override public function get view() : IView
        {
            return this.initView();
        }

        override protected function initView() : IView
        {
            if (this._npcShopMallModule == null)
            {
                this._npcShopMallModule = new NPCShopMallModule();
                this._npcShopMallModule.addEventListener(WindowEvent.SHOW, this.onAddedToStageHandler);
            }
            return this._npcShopMallModule;
        }

        private function onAddedToStageHandler(event:Event) : void
        {
            this.onMoneyUpdateHandler();
            
        }

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateHandler);
            NetDispatcher.addCmdListener(ECmdCell._ECmdCellUpdateShopItem, this.onNpcShopBroadCastHandler);
            
        }

        private function onMoneyUpdateHandler(param1:SMoney = null) : void
        {
            if (this._npcShopMallModule && this._npcShopMallModule.isHide == false)
            {
                this._npcShopMallModule.update(cache.role.money);
            }
            
        }

        private function onShopBuyNpcItemHandler(event:DataEvent) : void
        {
            var _loc_2:* = this._npcShopInfo.npcShop.entityId;
            var _loc_3:* = event.data["itemCode"] as int;
            var _loc_4:* = event.data["itemCount"] as int;
            GameProxy.shopProxy.buyNpcShopItem(_loc_2, _loc_3, _loc_4);
            
        }

        private function onShopBuyNpcItemSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:String = "";
            var _loc_3:* = event.data["itemCode"] as int;
            var _loc_4:* = event.data["itemCount"] as int;
            var _loc_5:* = ItemConfig.instance.getInfoByCode(_loc_3);
            if (ItemConfig.instance.getInfoByCode(_loc_3) && _loc_5.item)
            {
                _loc_2 = _loc_5.item.name;
            }
            MsgManager.addBroadCast("成功购买了" + _loc_4 + "个" + _loc_2);
            
        }

        private function onOpenNpcShopMallHandler(event:DataEvent) : void
        {
            this._npcShopInfo = event.data as NpcShopInfo;
            this.initView();
            if (this._npcShopInfo)
            {
                this._npcShopMallModule.updateDataProvider(this._npcShopInfo.npcShop.shopItem.shopItems);
            }
            this._npcShopMallModule.show();
            
        }

        private function onNpcShopBroadCastHandler(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SNpcShopItemUpdate;
            if (_loc_2 && this._npcShopInfo && EntityUtil.toString(_loc_2.entityId) == EntityUtil.toString(this._npcShopInfo.npcShop.entityId))
            {
                if (this._npcShopMallModule)
                {
                    this._npcShopMallModule.updateShopItem(_loc_2);
                }
            }
            
        }

        private function onGirdWalkEnd(event:PlayerEvent) : void
        {
            var _loc_3:IEntity = null;
            var _loc_4:Number = NaN;
            var _loc_2:* = RolePlayer.instance.currentPoint;
            if (this.view.isHide == false)
            {
                _loc_3 = GameController.scene.selectEntity;
                if (_loc_3)
                {
                    _loc_4 = Math.sqrt(Math.pow(_loc_3.x - _loc_2.x, 2) + Math.pow(_loc_3.y - _loc_2.y, 2));
                    if (_loc_4 > 120)
                    {
                        this.view.hide();
                    }
                }
            }
            
        }

        private function onSceneUpdateHandler(event:DataEvent) : void
        {
            if (this._npcShopMallModule && this._npcShopMallModule.isHide == false)
            {
                this._npcShopMallModule.hide();
            }
            
        }

    }
}
